// 权限相关API

import request from '@/utils/request'

export default {
  // 获取权限列表
  getPermissionList(params) {
    return request({
      url: '/dp-server/authority-server/permission/list',
      method: 'get',
      params: params
    })
  },
  // 根据角色获取权限
  getPermissionsByRole(roleId) {
    return request({
      url: `/dp-server/authority-server/permission/role/${roleId}`,
      method: 'get'
    })
  },
  // 更新角色权限
  updateRolePermissions(data) {
    return request({
      url: '/dp-server/authority-server/permission/update',
      method: 'post',
      data: data
    })
  },
  // 获取用户权限
  getUserPermissions(userId) {
    return request({
      url: `/dp-server/authority-server/permission/user/${userId}`,
      method: 'get'
    })
  }
}

export function getCurrentUserPermissions() {
  return request({
    url: '/dp-server/authority-server/permission/current-user',
    method: 'get',
    params: { 
      refresh: true
    },
    // 添加响应拦截器
    transformResponse: [data => {
      // 确保返回值始终是数组类型
      return Array.isArray(data) ? data : [];
    }]
  }).catch(error => {
    console.error('获取当前用户权限失败:', error);
    // 返回空数组作为默认值
    return [];
  });
}
